//
//  LBWComposeView.swift
//  weibo
//
//  Created by Limonwei on 15/12/12.
//  Copyright © 2015年 heima. All rights reserved.
//
//主视图点击加号进入的视图

import UIKit
import pop

class LBWComposeView: UIView {
    
    var target: UIViewController?
    
    //六个按钮的数组
    private lazy var buttons: [LBWComposeButton] = [LBWComposeButton]()
    
    //内部所有按钮显示的信息
    private var composeButtonInfo: [LBWComposeButtonInfo]?
    //设置界面
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //宽高
        width = SCREENW
        height = SCREENH
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        //添加子控件
        addSubview(bgImageView)
        addSubview(sloganImage)
        
        //添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self).offset(UIEdgeInsetsZero)
        }
        
        sloganImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        
        //添加按钮
        addChildButton()
    }

  
    //添加6个子按钮
    private func addChildButton(){
        
        //定义按钮的宽高
        let childW: CGFloat = 80
        let childH: CGFloat = 110
        
        //计算每个按钮之间的间距
        let childMarign: CGFloat = (SCREENW - 3 * childW) / 4
        
        composeButtonInfo = getComposeButtonInfo()
        
        //遍历每个按钮 设置属性
        for (i,value) in composeButtonInfo!.enumerate(){
            //初始化button
            let button = LBWComposeButton()
            
            //添加点击时间
            button.addTarget(self, action: "composeButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            //设置图片文字
            let icon = value.icon!
            let title = value.title!
            //指定显示的内容
            button.setImage(UIImage(named: icon), forState: UIControlState.Normal)
            button.setTitle(title, forState: UIControlState.Normal)
            
            //指定大小
            button.size = CGSize(width: childW, height: childH)
            
            //求出当前遍历添加的按钮是在哪一行哪一列
            let col = i % 3
            let row = i / 3
            
            button.x = CGFloat(col+1)*childMarign + CGFloat(col)*childW
            
            //加个屏幕的高度让它默认在屏幕外不显示 动画显示出来
            button.y = CGFloat(row) * (childMarign + childW) + SCREENH
            
            //添加按钮到视图上
            addSubview(button)
            //添加到数组中
            buttons.append(button)
        }
    }
    
    //获取按钮信息
    private func getComposeButtonInfo() -> [LBWComposeButtonInfo] {
        
        //读取plise文件
        let file = NSBundle.mainBundle().pathForResource("compose", ofType: "plist")!
        
        //转成数组
        let composeButtonInfos = NSArray(contentsOfFile: file)!
        
        //定义返回值
        var result = [LBWComposeButtonInfo]()
        
        //遍历字典 转模型
        for value in composeButtonInfos {
            result.append(LBWComposeButtonInfo(dict: value as! [String: AnyObject]))
        }
        
        //设置返回值
        return result
    }
    
    
    //执行动画
    private func anim(button: UIButton, isUp: Bool = true, index: Int){
        
        let anim = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        //设置终点的位置
        let y: CGFloat = isUp ? button.center.y - 350 : button.center.y + 350
        
        anim.toValue = NSValue(CGPoint: CGPoint(x: button.center.x, y: y))
        
        //弹性度 越大抖动越厉害
        anim.springBounciness = 8
        
        //弹性速度
        anim.springSpeed = 10
        
        //开始时间
        anim.beginTime = CACurrentMediaTime() + Double(index) * 0.025
        
        button.pop_addAnimation(anim, forKey: nil)
    }
    
    //外界访问方法  外界调用此方法将空间显示到屏幕上
    func show(target: UIViewController?){
        self.target = target
        
        target?.view.addSubview(self)
        
        //遍历button数组
        for (i,value) in buttons.enumerate(){
            anim(value, index: i)
        }
        
    }
    
    //监听方法
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        //执行动画
        //遍历button数组
        for (i,value) in buttons.reverse().enumerate(){
            anim(value, isUp: false, index: i)
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(0.3 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { () -> Void in
            self.removeFromSuperview()
        })
    }
    
    //实现按钮点击方法
    @objc private func composeButtonClick(button: UIButton){
        
        //遍历执行动画
        UIView.animateWithDuration(0.25, animations: { () -> Void in
            for value in self.buttons {
                value.alpha = 0.2
                if value == button {
                    // 是当前的按钮,
                    // 执行放大透明
                    value.transform = CGAffineTransformMakeScale(2, 2)
                }else{
                    // 执行缩小透明
                    value.transform = CGAffineTransformMakeScale(0.3, 0.3)
                }
            }
            
            }){(_) -> Void in
                //弹出控制器
                let info = self.composeButtonInfo![button.tag]
                
                if let name = info.classname {
                    let cls = NSClassFromString(name) as? UIViewController.Type
                    let result = cls?.init()
                    self.target?.presentViewController(LBWNavigationController(rootViewController: result!), animated: true, completion: { () -> Void in
                        self.removeFromSuperview()
                    })
               
                }
        }
        
    }
    

    //懒加载子控件
    
    //虚化的背景  第三方pop框架实现
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage.ScreenShot().applyLightEffect())
    
    //宣传语图片
    private lazy var sloganImage: UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))

}


































